from django.db import models
from datetime import datetime
from users.models import UserProfile,Grade


class Course(models.Model):
    name = models.CharField("课程名",max_length=50,help_text="课程名")
    desc = models.CharField("课程描述",max_length=300,help_text="课程描述")
    detail = models.TextField("课程详情",help_text="课程详情")
    learn_times = models.IntegerField("学习时长(分钟数)",default=0,help_text="学习时长")
    students = models.IntegerField("学习人数",default=0,help_text="学习人数")
    fav_nums = models.IntegerField("收藏人数",default=0,help_text="收藏人数")
    image = models.ImageField("封面图",upload_to="courses/%Y/%m",max_length=100,null=True,blank=True,default="default.jpg",help_text="封面图")
    click_nums = models.IntegerField("点击数",default=0,help_text="点击数")
    tag = models.CharField('课程标签',default='',max_length=10,help_text="课程标签")
    add_time = models.DateTimeField("添加时间",default=datetime.now)
    you_need_know = models.CharField('课程须知',max_length=300,default='',help_text="课程须知")
    teacher_tell = models.CharField('老师告诉你',max_length=300,default='',help_text="老师告诉你")
    grade = models.ForeignKey(Grade,related_name="course",on_delete=models.CASCADE, verbose_name="所属班级", null=True, blank=True,help_text="所属班级")
    teacher = models.ForeignKey(UserProfile,verbose_name='讲师',null=True,blank=True,on_delete=models.CASCADE,help_text="讲师")

    class Meta:
        verbose_name = "课程管理"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    __repr__ = __str__


class CourseBanner(models.Model):
    course = models.ForeignKey(Course,verbose_name='课程',null=True,blank=True,on_delete=models.CASCADE)
    image = models.ImageField(upload_to="banners/", verbose_name="图片", null=True, blank=True)
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = '课程轮播图管理'
        verbose_name_plural = verbose_name


class HotSearchWords(models.Model):
    """
    热搜词
    """
    keywords = models.CharField(default="", max_length=20, verbose_name="热搜词")
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = '热搜词管理'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.keywords


class Lesson(models.Model):
    """
    课程章节
    """
    course = models.ForeignKey(Course,verbose_name='课程',on_delete=models.CASCADE,related_name="lessons",help_text="所属课程")
    name = models.CharField("章节名",max_length=100,help_text="章节名")
    add_time = models.DateTimeField("添加时间",default=datetime.now)

    class Meta:
        verbose_name = "章节管理"
        verbose_name_plural = verbose_name

    def __str__(self):
        return '《{0}》课程的章节 >> {1}'.format(self.course, self.name)


class Video(models.Model):
    """
    章节视频
    """
    lesson = models.ForeignKey(Lesson, verbose_name="章节",on_delete=models.CASCADE,related_name="videos",help_text="所属章节")
    name = models.CharField("视频名",max_length=100,help_text="视频名称")
    url = models.CharField('访问地址',default='',max_length=500,help_text="访问地址")
    learn_times = models.IntegerField("学习时长(分钟数)", default=0,help_text="学习时长")
    add_time = models.DateTimeField("添加时间", default=datetime.now)

    class Meta:
        verbose_name = "视频管理"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Resource(models.Model):
    course = models.ForeignKey(Course, verbose_name="课程",on_delete=models.CASCADE,related_name="resource",help_text="所属课程")
    name = models.CharField("名称",max_length=100,help_text="资源名称")
    downloads = models.FileField("资源文件",upload_to="course/resource/%Y/%m",max_length=100,help_text="资源文件")
    add_time = models.DateTimeField("添加时间", default=datetime.now)

    class Meta:
        verbose_name = "课程资源管理"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Question(models.Model):
    """
    题库
    """
    course = models.ForeignKey(Course, verbose_name=u"课程")
    questionType = models.CharField(max_length=2, choices=(("xz", u"选择题"), ("pd", u"判断题"), ("zg", u"问答")), default="xz", verbose_name=u"题目类型")
    content = models.TextField(verbose_name=u"题目内容")
    answer = models.TextField(verbose_name=u"正确答案")
    choice_a = models.TextField(verbose_name=u"A选项", default="A")
    choice_b = models.TextField(verbose_name=u"B选项", default="B")
    choice_c = models.TextField(verbose_name=u"C选项", default="C")
    choice_d = models.TextField(verbose_name=u"D选项", default="D")
    score = models.IntegerField(verbose_name=u"分值", default=0)
    note = models.TextField(verbose_name=u"备注信息", default= u"问答题在此处做答")
    boolt = models.TextField(verbose_name=u"判断正误正确选项", default= "True")
    boolf = models.TextField(verbose_name=u"判断正误错误选项", default= "False")
    add_time = models.DateField(default=datetime.now, verbose_name=u"添加时间")

    class Meta:
        verbose_name = u"题库管理"
        verbose_name_plural = verbose_name

    def get_content_display(self, field):
        return self.content

    def __str__(self):
        return "{0}(题干:{1} | 正确答案:{2} )".format(self.course.name, self.content, self.answer)

    __repr__ = __str__


class PaperList(models.Model):
    """
    试卷列表
    """
    course = models.ForeignKey(Course, verbose_name=u"所属课程",related_name="paperlist")
    name = models.CharField(max_length=100, verbose_name=u"试卷名", default=u"")
    is_allow = models.BooleanField(default=False, verbose_name=u"是否启用")
    add_time = models.DateField(default=datetime.now, verbose_name=u"添加时间")

    class Meta:
        verbose_name = u"试卷列表管理"
        verbose_name_plural = verbose_name

    def __str__(self):
        return u"{0}(试卷名称:{1} | 使用状态:{2})".format(self.course.name, self.name, self.is_allow)

    __repr__ = __str__


class Paper(models.Model):
    """
    试卷名称
    """
    course = models.ForeignKey(Course, verbose_name=u"所属课程")
    question = models.ForeignKey(Question, verbose_name=u"题目")
    paper_name = models.ForeignKey(PaperList, verbose_name=u"试卷名称",related_name="papers")
    add_time = models.DateField(default=datetime.now, verbose_name=u"添加时间")

    class Meta:
        verbose_name = u"试题列表管理"
        verbose_name_plural = verbose_name

    def __str__(self):
        return u"{0} ({1})".format(self.paper_name, self.question.content)

    __repr__ = __str__
